package com.example.travel.controller;

import cn.hutool.core.date.DateUtil;
import com.example.travel.common.AjaxResult;
import com.example.travel.domain.entity.Comment;
import com.example.travel.domain.response.CommentAndUserResponse;
import com.example.travel.interceptor.TokenUtils;
import com.example.travel.service.CommentService;
import io.swagger.annotations.Api;
import nonapi.io.github.classgraph.json.Id;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author : LinQingLiang
 * @file : CommentController.java
 * @Since : 2023/2/15 18:03
 */

@RestController
@RequestMapping("/comment")
@Api(tags = "评论")
public class CommentController {


    @Autowired
    private CommentService commentService;

    @GetMapping("/tree/{articleId}")
    public AjaxResult findTree(@PathVariable Integer articleId){
        //查询所有的评论和回复数据
        List<CommentAndUserResponse> commentList = commentService.findCommentDetail(articleId);
        //查询只有评论的数据
        List<CommentAndUserResponse> originList = commentList.stream().filter(comment -> comment.getOriginId()==null).collect(Collectors.toList());
        //评论数据的子节点也就是评论内容
        for (CommentAndUserResponse origin:originList) {
            List<CommentAndUserResponse> comments = commentList.stream().filter(comment -> origin.getId().equals(comment.getOriginId())).collect(Collectors.toList());
            comments.forEach(comment ->{
                //找到父级评论的用户id和用户昵称，并设置给当前的回复对象
                commentList.stream().filter(t1 -> t1.getId().equals(comment.getPid())).findFirst().ifPresent((v ->{
                    comment.setPUserId(v.getUserId());
                    comment.setPNickName(v.getNickName());
                }));
            });

            origin.setChildren(comments);
        }
        return AjaxResult.success(originList);
    }


    @PostMapping("/insertComment")
    public AjaxResult insertComment(@RequestBody Comment comment){
        if(comment.getId() == null){
            comment.setUserId(TokenUtils.getUser().getId());
            comment.setTime(DateUtil.now());

            if (comment.getPid() != null){
                Integer pid = comment.getPid();
                Comment pComment = commentService.getById(pid);
                //如果当前回复的父级有祖宗，那么久设置相同的祖宗
                if (pComment.getOriginId() != null) {
                    comment.setOriginId(pComment.getOriginId());
                }else{
                    //没有就设置当前的父id为当前回复的祖宗
                    comment.setOriginId(pComment.getId());
                }
            }
        }
        return AjaxResult.success(commentService.save(comment));
    }

    @DeleteMapping("/{id}")
    public AjaxResult deleteById(@PathVariable Integer id){
        return AjaxResult.success(commentService.removeById(id));
    }
}
